package com.hzdl.cinema.Service.impl;

import com.hzdl.cinema.Service.TicketService;
import com.hzdl.cinema.dao.*;
import com.hzdl.cinema.dao.impl.*;
import com.hzdl.cinema.domain.*;


import java.math.BigDecimal;
import java.util.List;

public class TicketServiceImpl implements TicketService {
    PageBean<Ticket> pageBean = new PageBean<>();

    TicketDao ticketDao = new TicketDaoImpl();
    StudioDao studioDao = new StudioDaoImpl();
    SeatDao seatDao = new SeatDaoImpl();
    ScheduleDao scheduleDao = new ScheduleDaoImpl();
    PlayDao playDao = new PlayDaoImpl();

//    @Override
//    public PageBean<Ticket> findMyTicket(int userId, int currentPage, int rows) {
//        //        总条数
//        int totalCount = ticketDao.findCountByUserId(userId);
//        pageBean.setTotalCount(totalCount);
//        //        总页数
//        int totalPage = totalCount % rows == 0 ? totalCount / rows : totalCount / rows + 1;
//
//        //        当前页
//        pageBean.setCurrentPage(currentPage);
//
//        //        每页条数
//        pageBean.setRows(rows);
//
//        //        当前页信息
//        int index = (currentPage - 1) * rows;
//        List<Ticket> tickets = ticketDao.findMyTicket(userId,index,rows);
//        pageBean.setList(tickets);
//        return pageBean;
//
//    }

    @Override
    public void returnTicket(int ticketId) {
        ticketDao.returnTicket(ticketId);
    }

    @Override
    public PageBean<Ticket> findPage(int userId, int currentPage, int rows) {
//        总条数
        int totalCount = ticketDao.findCountByUserId(userId);
        pageBean.setTotalCount(totalCount);
//        总页数
        int totalPage = totalCount % rows == 0 ? totalCount / rows : totalCount / rows + 1;
        pageBean.setTotalPage(totalPage);
//        当前页
        pageBean.setCurrentPage(currentPage);
//        每页条数
        pageBean.setRows(rows);
//        当前页信息
        int index = (currentPage - 1) * rows;
        List<Ticket> ticketList = ticketDao.findPageList(userId, index, rows);
        if (ticketList==null){
            return null;
        }
        for (Ticket ticket : ticketList) {
            Studio studio = studioDao.findMyStudio(ticket.getStudioId());
            Schedule schedule = scheduleDao.findMySchedule(ticket.getScheduleId());
            Seat seat = seatDao.findMySeat(ticket.getSeatId());
            Play play = playDao.findMyPlay(schedule.getPlayId());
            ticket.setPlay(play);
            ticket.setSchedule(schedule);
            ticket.setSeat(seat);
            ticket.setStudio(studio);
        }
        pageBean.setList(ticketList);
        return pageBean;
    }


    @Override
    public PageBean<Ticket> seeTicke(Integer rows, Integer currentPage) {

        //    totalCount;//总条数
        int totalCount = ticketDao.totalTicke();
        pageBean.setTotalCount(totalCount);
        //    totalPage;//总页数
        int totalPage = totalCount % rows == 0 ? totalCount / rows : (totalCount / rows) + 1;
        pageBean.setTotalPage(totalPage);
        //    currentPage;//当前页
        pageBean.setCurrentPage(currentPage);
        //    rows;//每页的条数
        pageBean.setRows(rows);
        //list
        int index = (currentPage - 1) * rows;

        List<Ticket> list = ticketDao.fiedTicke(index, rows);
        pageBean.setList(list);

        return pageBean;
    }

    @Override
    public BigDecimal numTicket() {
        BigDecimal bigDecimal = ticketDao.numTicket();
        return bigDecimal;
    }
}
